import {
  listComIntellectual,
  getComIntellectual,
  delComIntellectual,
  addComIntellectual,
  updateComIntellectual,
} from "@/apis/user/intellectual";
import { getCookiesUserInfo } from "@/utils/auth";

export const api = {
  list: async (queryParams) => {
    let params = Object.assign({}, queryParams);
    params.companyId = JSON.parse(getCookiesUserInfo()).companyId;
    if (queryParams.applicationDate && queryParams.applicationDate.length > 1) {
      params.applicationDateStart = queryParams.applicationDate[0];
      params.applicationDateEnd = queryParams.applicationDate[1];
      params.applicationDate = null;
    }

    let res = await listComIntellectual(params);
    return res;
  },
  one: getComIntellectual,
  delete: delComIntellectual,
  add: async (queryParams) => {
    queryParams.companyId = JSON.parse(getCookiesUserInfo()).companyId;
    let res = await addComIntellectual(queryParams);
    return res;
  },
  update: updateComIntellectual,
  download: (queryParams) => {
    this.download(
      "company/ComIntellectual/export",
      {
        ...queryParams,
      },
      `ComIntellectual_${new Date().getTime()}.xlsx`
    );
  },
};

const MetaData = [
  {
    title: "ID",
    dataIndex: "id",
    dataType: "Number",
    hidden: true,
    inQuery: false,
    inTable: true,
    inForm: true,
    width: 50,
    fixed: true,
  },
  {
    title: "名称",
    dataIndex: "name",
    dataType: "Text",
    inQuery: true,
    inTable: true,
    inForm: true,
    fixed: true,
  },
  {
    title: "类型",
    dataIndex: "type",
    dataType: "Dict",
    inQuery: true,
    inTable: true,
    inForm: true,
    fixed: true,
    options: [
      {
        value: 0,
        label: "专利",
      },
      {
        value: 1,
        label: "商标",
      },
      {
        value: 2,
        label: "版权",
      },
    ],
  },
  {
    title: "申请日期",
    dataIndex: "applicationDate",
    dataType: "Date",
    inQuery: true,
    inTable: true,
    inForm: true,
  },
  {
    title: "注册日期",
    dataIndex: "registrationDate",
    dataType: "Date",
    inQuery: false,
    inTable: true,
    inForm: true,
  },
  {
    title: "到期日期",
    dataIndex: "expirationDate",
    dataType: "Date",
    inQuery: false,
    inTable: true,
    inForm: true,
  },
  {
    title: "法律状态",
    dataIndex: "legalStatus",
    dataType: "Dict",
    inQuery: true,
    inTable: true,
    inForm: true,
    options: [
      {
        value: 0,
        label: "申请中",
      },
      {
        value: 1,
        label: "已注册",
      },
      {
        value: 2,
        label: "已过期",
      },
    ],
  },
  {
    title: "权利范围",
    dataIndex: "scopeRights",
    dataType: "Text",
    inQuery: false,
    inTable: true,
    inForm: true,
  },
  {
    title: "发明人/创作者",
    dataIndex: "rightsHolder",
    dataType: "Text",
    inQuery: false,
    inTable: true,
    inForm: true,
  },
  {
    title: "申请人",
    dataIndex: "applicant",
    dataType: "Text",
    inQuery: false,
    inTable: true,
    inForm: true,
  },
  {
    title: "专利号/注册号",
    dataIndex: "patentNumber",
    dataType: "Text",
    inQuery: false,
    inTable: true,
    inForm: true,
  },
  {
    title: "摘要",
    dataIndex: "abstractInfo",
    dataType: "Text",
    inQuery: false,
    inTable: false,
    inForm: true,
  },
  {
    title: "详细描述",
    dataIndex: "description",
    dataType: "Text",
    inQuery: false,
    inTable: false,
    inForm: true,
  },
  {
    title: "技术领域",
    dataIndex: "technical",
    dataType: "Text",
    inQuery: false,
    inTable: true,
    inForm: true,
  },
  {
    title: "关键词",
    dataIndex: "keywords",
    dataType: "Text",
    inQuery: false,
    inTable: false,
    inForm: true,
  },
  {
    title: "附件",
    dataIndex: "attachments",
    dataType: "File",
    inQuery: false,
    inTable: false,
    inForm: true,
  },
  {
    title: "状态",
    dataIndex: "status",
    dataType: "Dict",
    inQuery: false,
    inTable: true,
    inForm: true,
    options: [
      {
        value: 0,
        label: "不可用",
      },
      {
        value: 1,
        label: "可用",
      },
    ],
  },
];

export const queryItems = () => {
  let items = MetaData.filter((item) => {
    return item.inQuery;
  }).map((item) => {
    var newItem = {
      title: item.title,
      dataIndex: item.dataIndex,
    };
    // 默认组件
    if (item.dataType == "Number") {
      newItem.component = "Input-number";
    } else if (item.dataType == "Text") {
      newItem.component = "Input";
    } else if (item.dataType == "Dict") {
      newItem.component = "Select";
      newItem.componentProps = {
        options: item.options,
      };
    } else if (item.dataType == "Date") {
      newItem.component = "Range-picker";
    }
    return newItem;
  });
  return items;
};

export const columns = () => {
  let items = MetaData.filter((item) => {
    return item.inTable;
  }).map((item) => {
    var newItem = {
      title: item.title,
      dataIndex: item.dataIndex,
      key: item.dataIndex,
      align: "center",
      width: item.width || 200,
    };
    if (item.fixed) {
      newItem.fixed = item.fixed;
    }
    if (item.dataType == "Dict") {
      newItem.customRender = (text, record, index) => {
        let option = item.options.find((option) => {
          return option.value == record[item.dataIndex];
        });
        return option ? option.label : "-";
      };
    }
    return newItem;
  });
  items.push({
    title: "操作",
    dataIndex: "action",
    key: "action",
    align: "center",
    width: 100,
    fixed: "right",
    scopedSlots: { customRender: "action" },
  });
  return items;
};

export const formItems = () => {
  let items = MetaData.filter((item) => {
    return item.inForm && !item.hidden;
  }).map((item) => {
    var newItem = {
      title: item.title,
      dataIndex: item.dataIndex,
    };
    // 默认组件
    if (item.dataType == "Number") {
      newItem.component = "Input-number";
    } else if (item.dataType == "Text") {
      newItem.component = "Input";
    } else if (item.dataType == "Dict") {
      newItem.component = "Select";
      newItem.componentProps = {
        options: item.options,
      };
    } else if (item.dataType == "Date") {
      newItem.component = "Date-picker";
    } else if (item.dataType == "File") {
      newItem.component = "Upload";
    }
    return newItem;
  });
  return items;
};

export const rules = {
  name: [{ required: true, message: "必须输入名称" }],
  type: [{ required: true, message: "必须选择类型" }],
  applicationDate: [{ required: true, message: "必须输入申请日志" }],
  applicant: [{ required: true, message: "必须输入申请人" }],
  patentNumber: [{ required: true, message: "必须输入专利号/注册号" }],
  registrationDate: [{ required: true, message: "必须输入注册日期" }],
  legalStatus: [{ required: true, message: "必须输入法律状态" }],
  technical: [{ required: true, message: "必须输入技术领域" }],
};

export const formConfg = function () {
  var config = {
    group: 2,
    labelCol: { span: 6 },
    wrapperCol: { span: 12 },
  };
  var items = formItems();

  // 计算分列
  let half = items.length / 2;
  let yu = items.length % 2;
  if (yu > 0) {
    half = half + 1;
  }
  config.formItems = [items.slice(0, half), items.slice(half, items.length)];
  return config;
};
